<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <1050429284@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-5-21
*   +----------------------------------------------------------------------
*   | Link (  )
*   +----------------------------------------------------------------------
**/

/**
 * 设置商品的标签   类   使用这个类  首先获取商品的 ID   可以使用 AlterPrice成员函数 获取IDs  
 *  该类中 函数   传人ID
 *
 */
class  AlterTag {
	protected $tags, $tagsObj, $description, $proO2o, $product, $brand;
	
	public function __construct(){
		$this->description = M('ProductDescription');
		$this->product = M('Product' );
		$this->tags    = M('Tags');
		$this->tagsObj = M('TagsObj');
		$this->brand   = M('ProductBrand');
		$this->proO2o  = M('ProductO2o');
	}
	
	/**
	 *为一个标签添加一个或者多个商品   $key为标签的name 不传 或者传非法  返回空
	 */
 	public function setProToTag($ids = 0 ,$key = '' , $method = '+'){
		if($method != '+' && $method != '-') return '';
		if($key == '') return '';
		if(!is_string($key)) return '';
		//根据$key获取 标签的ID
		$map['name'] = array('like','%'.$key.'%');
		$tagIds = $this->tags ->where($map) ->field('id')->select(); 
		$tagId = $tagIds[0]['id'];
		
		$_ids = $ids ; $ids =array();
		if( !is_array($_ids))   $ids[0] = $_ids;  
		foreach( $ids as $v){
			$data['tags_id'] = $tagId;
			$data['obj_id'] = $v;  $res ;
			if($method == '+'){
				$res = $this->tagsObj -> where($data) ->field('id') ->select();
				if($res != NULL) continue;
				$res = $this->tagsObj->add($data) ;
			}
			else 
			   $res = $this->tagsObj -> where($data)->delete() ;
			if($res == false)  return 'false';
		}
		return 'true';
	}
	
	/**
	 *获取 某个标签 某个品牌下的  所有商品   的 name  以及 是否在  product_o2o表里面添加
	 * 返回值  格式  {'id'=>'product_id','name'=>'name','isadd'=>'2' or '1'}   
	 */
	public function getProNameFromTag($key, $brand){
		//这个标签下的产品
		$IdsTag = $this->getFromTag($key);  if($IdsTag == '') return '';
		//这个品牌下的产品
		$IdsBrand =  $this->getFromBrd($brand);   if($IdsBrand == '') return '';
		//获取 他们共同的产品ID
		$Ids = $this->getGtong($IdsBrand,$IdsTag);
		//返回结果 1.获取名称
		$ret = array(); $i = 0; 
	 	//2.获取是否添加状态
		foreach( $Ids as $Id){
			$map['product_id'] = array('EQ' ,$Id);
			$names = $this->description ->field('name') ->where($map) ->select();
			$ret[$i]['id'] = $Id;
			$ret[$i]['name'] = $names[0]['name'];
			$stats = $this->proO2o ->field('id') -> where($map) ->select();
			if($stats == NULL ) $ret[$i++]['isadd'] = '未添加';
			else $ret[$i++]['isadd'] = '已添加';
		}	
		return $ret;
	}
	/**
	 * 根据ID  判断这些商品 是否在 product_o2o中添加
	 */
	public function getIsAdd($Id){
			$map['product_id'] = array('EQ' ,$Id);
			$stats = $this->proO2o ->field('id') -> where($map) ->select();
			if($stats == NULL ) return '未添加';
			else return '已添加';
	}
	/**
	 *添加商品到  product_o2o 人IDs
	 */
	public function addProToO2o($key){
		if(is_array($key)){
			foreach($key as $v){
				$data['product_id'] = $v;
				$this->proO2o->add($data);
			}
		}else{
			$data['product_id'] = $key;
			$this->proO2o->add($data);
		}
	}
	/**
	 *根据ID 获取商品的名称
	 */
	public function getNameId($Id){
		$map['product_id'] = array('EQ' ,$Id);
		$ret = $this->description ->field ('name') ->where($map) ->select();
		if($ret) return $ret[0]['name'];
		return '';
	}

	
	/**
	 *获取 某个标签下面的所有 品牌
	 */
	public function getBrdNameFromTag($key){
		//这个标签下的产品
		$IdsTag = $this->getFromTag($key);  if($IdsTag == '') return '';
		//根据产品找品牌
		$Ids    =  implode(',', $IdsTag);
		$map['id'] = array('in', $Ids);
		$res  = $this->product ->field('brand_id') ->where($map) ->select();
		//组合返回结果
		$brandIds =array(); $i=0;
		foreach( $res as $v){ //转一维数组
			 $brandIds[$i++] = $v['brand_id'];				
		}
		//去重
		$brandIds = array_unique($brandIds); $ret =array();$ii =0;
		//查找 品牌名称
		for( $cc=0; $cc <$i;$cc++){
			if($brandIds[$cc] == NULL) continue;
			$map['id'] = $brandIds[$cc];
			$name      = $this-> brand->field('name')->where($map)->select();
			$ret[$ii++]= $name[0]['name'];
		}
		return $ret;
	}
	
	/**
	 *设置商品的上下架状态  //$key...商品ID   $sta...商品上下架状态
	 */
	public function setProSta($key, $sta = 0){
		if($key == NULL) return '';
		if($sta != 0 && $sta != 1 && $sta != 2) return '';
		$res = $this->product ->where('id='.$key)->setField('status',$sta);
		if($res == NULL ) return 'false';
		return 'true';
	}
	
	/**
	 *获取 两个一维整形数组中 共同的元素
	 */
	private function getGtong($a,$b){
		return array_intersect($a,$b);
		$c = array();
		sort($a,SORT_NUMERIC ); 
		sort($b,SORT_NUMERIC ); 
		//....
		return $c;
	}
	/**
	 * 增加一个标签 //标签的名称 必须唯一  也就是不能跟以前相同
	 */
	public function addTag($name='',$name1='',$name2='',$des='',$usr=0){
		if($name == '') return '';  
 		$names = $this->tags -> field('name') -> select();
		//判重复
		foreach($names as $v){
			if( $name == $v['name']) {
				$data1['info'] =  $des ;
				$data1['alias'] = $name1;
				$data1['code'] = $name2;
				$map['name'] = $v['name'];
				$res = $this->tags ->where($map) -> save($data1);
				return 'true';
			}
		}
		$data['name'] =  $name ;
		$data['info'] =  $des ;
		$data['user_id'] = $usr;
		$data['alias'] = $name1;
		$data['code'] = $name2;
		$res = $this->tags -> add($data);
		if($res == NULL) return 'false';
		return 'true';
	}
	/**
	 *删除一个标签  //根据标签的名称删除
	 */
	public function delTag($name=''){
		if($name == '') return '';
		$map['name'] = $name;
		$res = $this->tags ->where($map)->delete();
		if($res == NULL) return 'false';
		return 'true';
	}
	/*** 
	 * 为一个标签添加一个商品 传人IDs  根据标签名称
	 */
	public function addProToTag($key,$Ids=array()){
		$map['name'] = $key;
		$ret = $this->tags ->where($map)-> field('id') -> select();

		foreach( $Ids as $v){
			$data['tags_id'] = $ret[0]['id'];
///////////////////////添加更多
			$data['obj_id'] = $v;
			$this ->tagsObj ->add($data);
		}
	}
	/**
	 * 获取所有的标签 传参 表示 每行多少个
	 */
	public function getAllTag($n){
		$res = array(); $ret = array();
		$res = $this->tags ->field('name')->select();
		$count = count($res);
		
		for($y=0;;$y++){
			for($x=0 ;$x <$n;$x++){
				if(($y*$n+$x)>=count($res)) return $ret;
				$ret[$y][$x] = $res[$y*$n+$x];
			}	
		}
		return $ret;	
	}

	/** 
	 * 根据品牌字段获取产品IDs  传空返回空
	 */
 	private function getFromBrd($key=''){
		 $map =array();
		 if(""==$key) return ''; 
		 $map['name'] = array('like','%'.$key.'%');
			//添加更多条件   
		 //////////////////////////////////////////////////////////////////////////
		$id_brands = $this->brand->where($map)->field('id') ->select();
		$i=0; $ids=array();
		foreach($id_brands as $id_brand){
			$map['brand_id'] = $id_brand['id'];
			$ida = $this->product->where($map) ->field('id') ->select();
			foreach($ida as $v){
				$ids[$i++] = $v['id'];
			 }
		}
		return $ids;
	}
	
	/***
	 * 根据标签字段获取产品IDs     传空返回空
	 */
	public function getFromTag($key=''){
	      $map =array();
		  if(""!=$key)
		  	 	$map['name'] = array('like','%'.$key.'%'); 	
		  //添加更多条件   
		  //////////////////////////////////////////////////////////////////////////
		  $tag_ids = $this->tags ->where($map)->field('id') ->select();
		  $i=0 ; $ids =array();
		  foreach( $tag_ids as $v){
			   $map['tags_id'] = $v['id'];
			   $map['type']    = '1';
			   $ida =$this->tagsObj->where($map) ->field('obj_id') ->select();
			   foreach( $ida as $v1){
				     $ids[$i++] = $v1['obj_id'];  		   
			   }
		  }
		  return $ids;
	}
	
	/**
	 * 获取标签id,父标签id 它的name
	 * @return multitype:array|multitype:string
	 */
	 public function get_all_tag(){
		 $arr = array();
		 $arr = $this->tags ->field('id,parent_id,name') ->select();
	
		 return $arr;
	}	
	/**
	 *   递归标签父子关系
	 *  @param $id=0
	 *  @return multitype:array|multitype:string
	 */
    public function gettag($id){
		$map['parent_id'] = $id;  
		$ret = $this->tags ->where($map) ->field('id,name') ->select();
		foreach($ret as $v){
			$res = $this->gettag($v['id']);
			if(!$res) return $ret;
			else $ret = array_merge($ret, $res);

		}
		return $ret; 
	}
    /**
	 *   根据标签的名称获取这么标签的直系子标签的名称 
	 *  @param $name = ''
	 *  @return multitype:array
	 */
	public function gettn_from_name($name){
		$map['name'] = $name;
		$id = $this->tags->where($map) ->field('id')->select();
		$where['parent_id'] = $id[0]['id'];
		$ret =$this->tags->where($where)->field('name')->select();
		$res = array(); $i=0;
		foreach($ret as $v){
			$res[$i++]=$v['name'];
		}
		return $res;
	}
	/**
	 *  增删父子  
	 *  @param $id=0
	 *  @param $prant_id=0
	 *  @param $fang=+ | -
	 *  @return true|false
	 */
	public function aordtag($pid=0,$id=0,$fang="+"){
		if($fang !="+" && $fang != "-") return false;
		if($fang == "+"){
			$where['id'] = $pid;
			$ppath = $this->tags->where($where)->field('path')->select();
			if(!$ppath)
				$path = $pid.','.$ppath[0]['path'];
			else $path = $pid;
			$map['id'] = $id;
			$map['parent_id'] = $pid;
			$map['path'] = $path;
			$res = $this->tags->save($map);
		}else{
			$map['id'] = $id;
			$_path = $this->tags->where($where)->field('path')->select(); $path = $_path[0]['path'];
			$path = preg_replace('/[,'.$pid.']+/i','',$path);
			$pid =  substr ($path,strripos(','));
			$map['parent_id'] = $pid;
			$map['path'] = $paht;
			$res = $this->tags->save($map);
		}
		return true;
	}
	/**
	 * 获取标签id下的所有产品 的  ID
	 *  @param $id=0
	 *  @return array
	 */
	public function getprofromid($id){
	    $map['tags_id'] = $id;
	    $map['type']    = '1';
	    $ida =$this->tagsObj->where($map) ->field('obj_id') ->select();
		$ids =array();$i=0;
	    foreach( $ida as $v){
			 $ids[$i++] = $v['obj_id'];  		   
	    }
		return $ids;	
	}
	/**
	 * 获取所有最高级标签
	 */
	 public function getallgtag(){
		$map['parent_id'] = 0;
		$ret = $this->tags->where($map)->field('name')->select();
		return $ret;
	 }
	
	/**
	 * 二维数组返回一维或字符串
	 * @param array $_arr
	 * @param string $_field
	 * @param string $_type 1为数组 2为字符串
	 * @return multitype:array|multitype:string
	 */
	function arr2to1($_arr,$_field = '0',$_type = '1'){
		if ( ! $_arr ) return array();
		$arrayids = array(); $i = 0;
		foreach ($_arr as $k => $v){
			$arrayids[$i++] = $v[$_field];
		}
		if($_type != '1') $arrayids = implode(',',$arrayids);
		
		return $arrayids;
	}
}